'''
多进程锁, 多线程锁都要自己实现一遍
多进程通过Queue来实现进程通信
把上述概念熟记并理解
'''
import requests
import time
from threading import Thread
from multiprocessing import Process, Lock,Queue

def speak_num(num, lock):
    with lock:
        for i in range(num):
            print(f"当前在说数字：{i}")

def process_speak_num():
    process_array = []
    old = time.time()
    lock = Lock()
    for i in range(7):
        p = Process(target=speak_num, args=(i, lock))
        process_array.append(p)
        p.start()
    for p in process_array:
        p.join()
    now = time.time()
    print(f"进程说：KO!!！用时{round((now-old),3)}s")

def thread_speak_num():
    thread_array = []
    old = time.time()
    lock = Lock()
    for i in range(7):
        t = Thread(target=speak_num, args=(i, lock))
        thread_array.append(t)
        t.start()
    for t in thread_array:
        t.join()
    now = time.time()
    print(f"进程说：KO!!！用时{round((now-old),3)}s")
def queue_num(num, my_queue):
    my_queue.put(num)
def queue_speak_num():
    process_array = []
    old = time.time()
    my_queue = Queue()
    for i in range(7):
        p = Process(target=queue_num, args=(i, my_queue))
        process_array.append(p)
        p.start()
    for p in process_array:
        p.join()
    now = time.time()
    print(f"Queue说：KO!!！用时{round((now - old), 3)}s")
    n = 0
    while n <= 6:
        print(my_queue.get())
        n += 1

if __name__ == "__main__":
    process_speak_num()
    thread_speak_num()
    queue_speak_num()